package com.hyts.bmsp.session;

import javax.servlet.http.HttpServletRequest;

import com.hyts.bmsp.error.ProcessError;
import org.apache.log4j.Logger;

import com.alibaba.druid.util.StringUtils;

/**
 * @ClassName: WebSessionManager
 * @Description: webSession对象实现Jfinal框架的：com.jfinal.plugin.auth.ISession
 * @author: 李博/Alex
 * @date: 2017-2-14 下午08:15:47
 */
public final class WebSessionManager{
	
	
	public static final String SESSION_KEY_TOKEN = "USER_TOKEN_KEY";
	
	public static final String SESSION_VALUE_TOKEN = "USER_TOKEN_VALUE";
	
	public static final String SESSION_VALUE_TOKEN_ID = "USER_TOKEN_ID";
	
	/**
	 * @fieldName: logger
	 * @fieldType: Logger
	 * @Description: 日志输出器
	 */
	private static final Logger logger = Logger.getLogger(WebSessionManager.class);
	
	/**
	 * @Title: setAttribute
	 * @Description: 插入对应的Session信息
	 * @param request
	 * @param key
	 * @param value
	 * @return
	 * @return: String
	 */
	public static void setAttribute(HttpServletRequest request,String key,Object value){
		
		logger.debug("开始注入:{key:"+key+" value:"+value+"}");
		
		if(StringUtils.isEmpty(key) || value == null)
		{
			throw new ProcessError("传入的Key或者value为空!");
		}
		if(request == null)
		{
			throw new ProcessError("传入的HttpServletRequest对象为空!");
		}
	    request.getSession().setAttribute(key, value);
	}
	
	
	/**
	 * @Title: getAttribute
	 * @Description: 获取对应key的value值
	 * @param request
	 * @param key
	 * @return
	 * @return: Object
	 */
	public static Object getAttribute(HttpServletRequest request,String key){
		
		logger.debug("开始获取:{key:"+key+"value值}");
		
		if(StringUtils.isEmpty(key))
		{
			throw new ProcessError("传入的Key为空!");
		}
		if(request == null)
		{
			throw new ProcessError("传入的HttpServletRequest对象为空!");
		}
		return request.getSession().getAttribute(key);
	}
	
	
	/**
	 * @Title: removeAttribute
	 * @Description: 删除Session对应的key的信息
	 * @param request
	 * @param key
	 * @return: void
	 */
	public static void removeAttribute(HttpServletRequest request,String key){
	
		logger.debug("开始删除:{key:"+key+"value值}");
		
		if(StringUtils.isEmpty(key))
		{
			throw new ProcessError("传入的Key为空!");
		}
		if(request == null)
		{
			throw new ProcessError("传入的HttpServletRequest对象为空!");
		}
		request.getSession().removeAttribute(key);
	}
	
	/**
	 * @Title: logout
	 * @Description: 执行登出操作注销信息
	 * @param request
	 * @param key
	 * @return
	 * @return: Object
	 */
	public static void logout(HttpServletRequest request){
		
		logger.debug("开始执行注销Session操作:doLogout");
		
		if(request == null)
		{
			throw new ProcessError("传入的HttpServletRequest对象为空!");
		}
	    request.getSession().invalidate();
	}
	
	

}
